/**
* 用来提供操作附件域的函数，因为收文管理和其他业务有一些不同
* 故单独提供一个文件
* 新增附件 -----------  addFile()
* 删除附件 -----------  deleteFile(fileIndex)
*                       参数fileIndex是附件的序号
* 显示附件列表--------  attachGrid()]
* 查看附件 -----------  editFile(index)
*                       参数index是附件的序号
*/

var curRow="";
//确定鼠标所在行
function document.onclick()
{
   var e = event.srcElement
   while (e=e.parentElement)
   {
     if(e.tagName == "TR"){
       curRow=e.rowIndex;
       break;
      }
   }
}
function editFile(index)
{
 if(index<0) return;
 var tempObj=attach.fndbyIndex(index);
 if(tempObj==null) return;

 if(tempObj.getServer_fileFlg()=="Y")
 {
    var downFlg=false;
    try{
      form1.Attachment.DownloadAttachment(tempObj.getURL(),tempObj.getAttachID()+"."+tempObj.getExtendName());
      downFlg=true
    }catch(ex)
    {
      downFlg=false;
      alert("从服务器下载文件失败!");
    }
    if(downFlg==true)
    {
        tempObj.setServer_fileFlg("N");
        tempObj.setURL("C:\\temp\\WebOA\\"+tempObj.getAttachID()+"."+tempObj.getExtendName());
        form1.Attachment.OpenAttachment(tempObj.getURL());
    }
    setEditFlag();

 }
 else
 {
   form1.Attachment.OpenAttachment(tempObj.getURL());
 }
}

function addFile()
{
  var filepath=document.form1.filebox.value;
  if(filepath!="")
  {
    if(attach.fndbyFilePath(filepath)==null){
      attach.addAttach(filepath,"",strHelper.getFileName(filepath),
           strHelper.getExtendName(filepath),"0","","","","","N",filepath);
      attachGrid();
      setEditFlag();
    }
  }

}

function deleteFile(fileIndex)
{

   if(fileIndex<0) return;
   var st=attach.fndbyIndex(fileIndex);
   if(st!=null)
   {
     if(st.getServer_fileFlg()=="N")
       attach.delAttachByIndex(fileIndex);
     else
       st.setStatusFlg("Y");
   }
   attachGrid();
   setEditFlag();
}

/**
* 拆离附件时
* ◎index 是当前文件的位置索引号
*/
function caliFile(index)
{
 if(index<0) return;
 var tempObj=attach.fndbyIndex(index);
 if(tempObj==null) return;

 if(tempObj.getServer_fileFlg()=="Y")
 {
    //alert(tempObj.getURL());
    //alert(form1.attachOBJ.);
    var downFlg=false;
    try{

      window.location.href="/serviceLocator?action=breakAttachment&attachID="+tempObj.getAttachID()+"&businessType=FWGL&fieldName=attachment",tempObj.getAttachID()+"."+tempObj.getExtendName();
      downFlg=true
    }catch(ex)
    {
      downFlg=false;
      alert("从服务器下载文件失败!");
    }
 }
 else
 {
   alert("当前文档是本地文件，不需拆理!")
 }
}
/**
*把accach对象中的File写入表格
*/
function attachGrid()
{
  var count=mytab.rows.length;
  for(i=count-1;i>1;i--)
  {
    mytab.deleteRow(i);
  }
  if(attach==null) return;
  for(i=0;i<attach.getAttachCount();i++)
  {
   var tmpObj=attach.fndbyIndex(i);
   var c1
   if(tmpObj!=null)
   {
     //addrow(tmpObj.getFilePath());
       var count=mytab.rows.length+1;
       r=mytab.insertRow();
       if(tmpObj.getStatusFlg()=="Y"){
         c1 = r.insertCell();
         c1.innerHTML="<font color=red>"+(count-2)+"</font>";
         c1.align = "center";
         c1.valign="middle";
         c1.height="25"
         c1.width="10"
         r.insertCell().innerHTML="<font color=red>"+tmpObj.getFilePath()+"</font>";
         r.insertCell().innerHTML="<img src='/img/eml/trash_box.gif'>";
       }else{
         c1 = r.insertCell();
         c1.innerHTML=count-2;
         c1.align = "center";
         c1.valign="middle";
         c1.height="25"
         c1.width="10"
         if(tmpObj.getServer_fileFlg()!="Y")
         {
          r.insertCell().innerHTML=tmpObj.getFilePath();
         }else{
          r.insertCell().innerHTML="<a href=javascript:openHref('"+tmpObj.getURL()+"')>"+tmpObj.getFilePath()+"</a>";
         }
          r.insertCell().innerHTML="<a href=javascript:caliFile('"+i+"')>拆离</a> &nbsp;<a href=javascript:editFile('"+i+"')>编辑</a> &nbsp;<a href=javascript:deleteFile('"+i+"')>删除</a>";
     }
       r.id="r"+count;
   }
  }
}

function openHref(urls){
  window.open(urls,'','width=400,height=225,scrollbars=yes,resizable=yes,center:yes');
}